package com.ithuangyl.common.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ithuangyl.common.R;
import com.ithuangyl.common.loader.LoaderCreator;
import com.wang.avi.AVLoadingIndicatorView;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/7/7 20:15
 */
public class LoaderDialog extends BaseDialogFragment {
    @Override
    Object getLayout() {
        return R.layout.dialog_loader;
    }

    private String type;

    public static LoaderDialog create(String type) {
        LoaderDialog dialog = new LoaderDialog();
        Bundle args = new Bundle();
        args.putString("type", type);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            this.type = getArguments().getString("type");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        LoaderCreator.create(type, getActivity(), (AVLoadingIndicatorView) view.findViewById(R.id.id_loadingView));
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        final Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.setCanceledOnTouchOutside(false);
            setCancelable(false);
        }
    }
}
